package client;

import java.util.List;

import org.eclipse.swt.widgets.Display;

import client.AppWindow.GUIUpdater;

import dictionary.gameEntities.*;
import dictionary.interfaceData.*;
import dictionary.interfaceData.NetworkDataWrapper.ActionType;


/**
 * status: finished
 *
 * @author mjp
 * @version 1.0
 * @created 06-Nov-2011 10:34:59 AM
 */
public class Client {
	private static final boolean GUI_IMPLEMENTED = false;
	private static final boolean DEBUG = true;
	
	private AppWindow gui;
	public ClientMessager messager;

	private Display display;
	
	public void out(String text)
	{
		if (DEBUG) System.out.println("Client ("+this.toString()+"): "+text);
	}
	
	public static void main(String[] args) {
		// defaults
		String ip = "127.0.0.1";
		int port = 4444;

		// provided on command line
		if (args!=null && args.length>=2)
		{
			ip = args[0];
			port = Integer.valueOf(args[1]);
		}
//		new Client(ip, port, new AppWindow());
	}
	
	/** 
	 * only for testing & debugging purposes.
	 * @deprecated
	 */
	public Client(String ipAddress, int port) {
		// create new ClientMessager using server ip address.
		// then just wait.  receiveDataFromServer will be called when 
		// the server has new information
		out("creating messager.");
		messager = new ClientMessager(this, ipAddress, port);
		out("messager created.");
	}

	public Client(String ipAddress, int port, Display display, AppWindow window) {
		// create new ClientMessager using server ip address.
		// then just wait.  receiveDataFromServer will be called when 
		// the server has new information
		out("creating messager.");
		messager = new ClientMessager(this, ipAddress, port);
		out("messager created.");
		gui = window;
		this.display = display;
	}

	private void updateGui(NetworkDataWrapper data) {
		AppWindow.GUIUpdater gameStateUpdater = gui.new GUIUpdater(data);
		display.asyncExec(gameStateUpdater);
	}

	//////////////////////////////////////////////////////////////////////////
	//  Server action responses
	//////////////////////////////////////////////////////////////////////////
	
	/**
	 * Based on the data received, make the appropriate change
	 * @param data
	 */
	public void receiveDataFromServer(NetworkDataWrapper data)
	{
		out("RECEIVED DATA FROM SERVER\n"+data.toString());
		updateGui(data);
	}
}//end Client